from django.db.models import Q

from user_center.models import User


class UserRepository:
    def get_users(self, **kwargs) -> (int, list[User]):
        q = Q()
        if username := kwargs.get("username"):
            q &= Q(username=username)
        if password := kwargs.get("password"):
            q &= Q(password=password)
        if email := kwargs.get("email"):
            q &= Q(email=email)
        if birthday := kwargs.get("birthday"):
            q &= Q(birthday=birthday)
        q &= Q(is_deleted=False)
        objs = User.objects.filter(q)
        total = objs.count()
        if (page_no := kwargs.get("page_no")) and (
            page_size := kwargs.get("page_size")
        ):
            offset = (page_no - 1) * page_size
            objs = objs[offset : offset + page_size]
        objs = objs.select_related("campus_identity").prefetch_related("logs")
        return total, list(objs)

    def save_or_update(self, user: User):
        user.save()


class CampusIdentityRepository:
    pass


class UserLogRepository:
    pass
